<?php
include('../classess/class_affiliate_user.php');
$new_user = new affUser();
if (isset($_POST['export_csv'])) {
    $new_user->export_csv($_POST);
}

include_once('header.php');
include_once('left_navigation.php');

if (isset($_GET['del_id'])) {

    $delete_activate_user = $new_user->del_user($_GET['del_id']);
}



if (isset($_GET['act_id'])) {

    $delete_activate_user = $new_user->activate_user($_GET['act_id']);
}



if (isset($_POST['aff_usr_id'])) {

    $get_single_course = $new_user->update_aff_wdrwamt($_POST['aff_usr_id']);
}



if (isset($_GET['id'])) {

    $get_single_course = $new_user->get_aff_user($_GET['id']);
}
?>

<?php
if (isset($_GET['id'])) {

    if ($_GET['id'] != '') {
        ?>

        <div class="col-lg-12"><!-- Start Middle Content Div-->

            <div class="box">



                <div class="box-header" data-original-title=""><!-- Start Box Header Div-->

                    <h2><i class="fa fa-edit"></i><span class="break"></span><?php echo 'Edit '; ?> Affilate User</h2>

                    <div class="box-icon">

                        <a href="gallery.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>

                        <a href="gallery.html#" class="btn-close"><i class="fa fa-times"></i></a>

                    </div>

                </div><!-- End Box Header Div-->

                <div class="box-content"><!-- Start Box Content Div-->

                    <div class="row">



                        <div class="col-sm-12">

                            <?php
                            if (isset($add_update_course)) {
                                ?>

                                <div class="alert alert-info">

                                    <div style="float: right; width: 5%;"><button type="button" class="close" data-dismiss="alert">&times;</button></div>

                                    <div style="float:left;width: 92%;word-wrap: break-word;"><?php echo $add_update_course; ?></div>

                                    <div style="clear:both;"></div>							

                                </div>

                                <?php
                            }
                            ?>

                            <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?aff_prg=1<?php
                            if (isset($_GET['id'])) {
                                echo '&id=' . $_GET['id'];
                            }
                            ?>" name="add_aff_user_form" id="add_aff_user_form" enctype="multipart/form-data">

                                <div class="main_form_box col-sm-6">

                                    <div class="form-content  form-content-form">								



                                        <label>Name</label>

                                        <input type="text" name="aff_username" id="aff_username" value="<?php
                                        if (isset($get_single_course['0']->username)) {
                                            echo $get_single_course['0']->username;
                                        }
                                        ?>" readonly> 



                                        <label>Email</label>

                                        <input type="text" name="aff_email" id="aff_email" value="<?php
                                        if (isset($get_single_course['0']->email_id)) {
                                            echo $get_single_course['0']->email_id;
                                        }
                                        ?>" readonly > 

                                        <label>Address</label>
										
                                        <input type="text" name="aff_address" id="aff_address" value="<?php
                                        if (isset($get_single_course['0']->address)) {
                                            echo $get_single_course['0']->address;
                                        }
                                        ?>"  > 



                                        <label>Website URL</label>

                                        <input type="text" name="aff_email" id="aff_email" value="<?php
                                        if (isset($get_single_course['0']->website_url)) {
                                            echo $get_single_course['0']->website_url;
                                        }
                                        ?>" readonly>

                                    </div>

                                </div>



                                <div class="main_form_box col-sm-6" >

                                    <div class="form-content  form-content-form">



                                        <label>Is Active</label>

                                        <select id="is_active" name="is_active">

                                            <option value="1" <?php
                                            if (isset($get_single_course['0']->is_active)) {
                                                if ($get_single_course['0']->is_active == 1)
                                                    echo 'selected="selected"';
                                            }
                                            ?>>Yes</option>

                                            <option value="0" <?php
                                            if (isset($get_single_course['0']->is_active)) {
                                                if ($get_single_course['0']->is_active == 0)
                                                    echo 'selected="selected"';
                                            }
                                            ?>>No</option>

                                        </select>



                                        <label>Withdrawl Minimum Amount</label>

                                        <input type="text" name="min_amt_wdrw" id="min_amt_wdrw" value="<?php
                                        if (isset($get_single_course['0']->withdraw_min_amount)) {
                                            echo $get_single_course['0']->withdraw_min_amount;
                                        }
                                        ?>"> 



                                        <label>Withdrawl Maximum Amount</label>

                                        <input type="text" name="max_amt_wdrw" id="max_amt_wdrw" value="<?php
                                        if (isset($get_single_course['0']->withdraw_max_amount)) {
                                            echo $get_single_course['0']->withdraw_max_amount;
                                        }
                                        ?>"> 



                                        <label style="text-align:right; margin-top:35px;">

                                            <button type="button" class="btn btn-blue login_button" name="edit_aff_user" id="edit_aff_user"><?php echo 'Edit'; ?></button> 							

                                        </label>

                                    </div>

                                </div>

                                <div style="clear:both;"></div>

                                <input type="hidden" name="aff_usr_id" id="aff_usr_id" value="<?php
                                if (isset($_GET['id'])) {
                                    echo $_GET['id'];
                                }
                                ?>">

                            </form>



                        </div><!--/col-->

                    </div><!-- End Row div -->

                </div><!-- End Box Content Div -->



            </div><!-- End Box Div-->

        </div><!-- End Middle Content Div-->

        <?php
    }
}
?>

<div class="col-lg-12"><!-- Start Middle Content Div-->

    <div class="box">

        <div class="row">		

            <div class="col-lg-12">

                <div class="box">

                    <div class="box-header" data-original-title>

                        <h2><i class="fa fa-user"></i><span class="break"></span>Affiliate Users</h2>

                        <div class="box-icon">

                            <a href="table.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>

                            <a href="table.html#" class="btn-close"><i class="fa fa-times"></i></a>

                        </div>

                    </div>

                    <div class="box-content">

                        <?php
                        if (isset($delete_activate_user)) {
                            ?>

                            <div class="alert alert-info">

                                <div style="float: right; width: 5%;"><button type="button" class="close" data-dismiss="alert">&times;</button></div>

                                <div style="float:left;width: 92%;word-wrap: break-word;"><?php echo $delete_activate_user; ?></div>

                                <div style="clear:both;"></div>							

                            </div>

                            <?php
                        }
                        ?>

                        <div class="search_box">

                            <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] . '?aff_prg=1'; ?>" id="search_form" name="search_form">

                                <div class="fl_left" style="width:50%;">

                                    <div class="fl_left sel_label_search" style="width:11%;">Search:</div>

                                    <div class="fl_left">

                                        <input type="text" name="search_value" id="search_value" placeholder="Search" value="<?php
                                        if (isset($_POST['search_value'])) {
                                            echo $_POST['search_value'];
                                        } else {
                                            echo '';
                                        } if (isset($_GET['srch_value'])) {
                                            echo $_GET['srch_value'];
                                        } else {
                                            echo '';
                                        }
                                        ?>">&nbsp;

                                        <input type="submit" name="submit_search" id="submit_search" class="btn btn-blue srch_btn" value="Search">

                                    </div>

                                </div>

                                <div class="fl_right">

                                    <div class="fl_left" style="width:15%; margin-top:5px;">Number Of Records : </div>

                                    <div class="fl_left">

                                        <?php
                                        $limit_10 = '';

                                        $limit_25 = '';

                                        $limit_50 = '';

                                        $limit_100 = '';

                                        if (isset($_POST['sel_limit'])) {

                                            if ($_POST['sel_limit'] == '10') {

                                                $limit_10 = "selected='selected'";
                                            }

                                            if ($_POST['sel_limit'] == '25') {

                                                $limit_25 = "selected='selected'";
                                            }

                                            if ($_POST['sel_limit'] == '50') {

                                                $limit_50 = "selected='selected'";
                                            }

                                            if ($_POST['sel_limit'] == '100') {

                                                $limit_100 = "selected='selected'";
                                            }
                                        }

                                        if (isset($_GET['sel_get_limit'])) {

                                            if ($_GET['sel_get_limit'] == '10') {

                                                $limit_10 = "selected='selected'";
                                            }

                                            if ($_GET['sel_get_limit'] == '25') {

                                                $limit_25 = "selected='selected'";
                                            }

                                            if ($_GET['sel_get_limit'] == '50') {

                                                $limit_50 = "selected='selected'";
                                            }

                                            if ($_GET['sel_get_limit'] == '100') {

                                                $limit_100 = "selected='selected'";
                                            }
                                        }
                                        ?>

                                        <select name="sel_limit" id="sel_limit" onchange="javascript:document.search_form.submit();">

                                         <!--   <option value="10" <?php echo $limit_10; ?> >10</option>
-->
                                            <option value="25" <?php echo $limit_25; ?> >25</option>

                                            <option value="50" <?php echo $limit_50; ?> >50</option>

                                            <option value="100" <?php echo $limit_100; ?> >100</option>

                                        </select>



                                    </div>



                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                    <input type="submit" name="export_csv" id="export_csv" class="btn btn-blue srch_btn" value="Export CSV">


                                </div>



                                <div class="clear_fix"></div>

                            </form>

                        </div>

                        <table class="table table-striped table-bordered">

                            <thead>

                                <tr>

                                    <th>User Name</th>

                                    <th>Email</th>

                                    <th>Phone</th>

                                    <th>Address</th>

                                    <th>City</th>

                                    <th>Country</th>

                                    <th>Web URL</th>
                                    <th>Date</th>

                                    <th>Total Amount($)</th>

                                    <th>Paid Amount($)</th>

                                    <th>Remaining Amount($)</th>
                                    <th>Payment Type</th>
                                    <th>Bank Name/Paypal</th>
                                    <th>Account No.</th>
                                    <th>Branch Name</th>
                                    <th>Pancard No.</th>


                                    <th>Is Active</th>

                                    <th>Action</th>

                                    <th></th>

                                </tr>

                            </thead>   

                            <tbody>

                                <?php
                                $rec_limit = 25;

                                if (isset($_POST['sel_limit'])) {

                                    $rec_limit = $_POST['sel_limit'];
                                }



                                if (isset($_GET['sel_get_limit'])) {

                                    $rec_limit = $_GET['sel_get_limit'];
                                }



                                $condition_search = '';

                                $value = '';

                                if (isset($_POST['search_value'])) {

                                    $value = trim($_POST['search_value']);



                                    if (!empty($value)) {

                                        $cond_where = 'AND ';

                                        $condition_search = $cond_where . " (username like '%" . $value . "%' OR email_id like '%" . $value . "%' OR phone like '%" . $value . "%' OR city like '%" . $value . "%' OR country like '%" . $value . "%')";
                                    }
                                }



                                if (isset($_GET['srch_value'])) {

                                    $value = $_GET['srch_value'];



                                    if (!empty($value)) {

                                        $cond_where = 'AND ';



                                        $condition_search = $cond_where . " (username like '%" . $value . "%' OR email_id like '%" . $value . "%' OR phone like '%" . $value . "%' OR city like '%" . $value . "%' OR country like '%" . $value . "%')";
                                    }
                                }



                                $select_count_course = "select count(*) from tbl_affiliate AS af 
                                                Left join tbl_bank_detail as bk ON bk.b_affilate_id = af.id " . $condition_search . "
                                                ORDER BY id DESC ";

                                $result_count_course = mysql_query($select_count_course);



                                $row = mysql_fetch_array($result_count_course);

                                $rec_count = $row[0];



                                if (isset($_GET['page'])) {

                                    $page = $_GET['page'] + 1;

                                    $offset = $rec_limit * $page;
                                } else {

                                    $page = 0;

                                    $offset = 0;
                                }



                                $serial_number = $page * $rec_limit;



                                $left_rec = $rec_count - ($page * $rec_limit);



                                /* $select_trainer = "SELECT * ,af.id as affiliate_id FROM tbl_affiliate as af  
                                  Left join tbl_bank_detail as bk ON bk.b_affilate_id = af.id " . $condition_search . "
                                  ORDER BY id DESC LIMIT " . $offset . ", " . $rec_limit;
                                 */
                                $total_commission = '';
                                $select_trainer = "select *,af.id AS affiliate_id,
                                                af.username AS affiliate_username,								
                                                af.address AS affiliate_address from tbl_affiliate AS af 
                                                Left join tbl_bank_detail as bk ON bk.b_affilate_id = af.id " . $condition_search . "
                                                ORDER BY id DESC LIMIT " . $offset . ", " . $rec_limit; 
                                
                                /*$select_trainer = "SELECT * ,af.id AS affiliate_id,
                                                af.username AS affiliate_username,								
                                                af.address AS affiliate_address,
                                                * SUM(c.`commission`) as total_commission FROM tbl_affiliate AS af 
                                                JOIN tbl_user AS u ON af.id = REPLACE(u.reffered_by,'a_','')
                                                JOIN tbl_commission AS c ON c.`trainee_id` = u.`id`
                                                JOIN tbl_user_details AS ud ON c.`trainee_detail_id` = ud.`id` 
                                                LEFT JOIN tbl_bank_detail AS bk ON bk.b_affilate_id = af.id 
                                                WHERE ud.`is_refund` = '0' AND u.`is_active` =  1  AND u.`is_paid` = 1 " . $condition_search . "
                                                GROUP BY af.id
                                                ORDER BY af.id DESC LIMIT " . $offset . ", " . $rec_limit;
                                */

                                $query_trainer = mysql_query($select_trainer) or die(mysql_error());



                                $i = 1;

                                if (mysql_num_rows($query_trainer) > 0) {
                                    $total_amount = '0';
                                    $pending_amount = '0';
                                    $paid_amount = 0;
                                    while ($row = mysql_fetch_array($query_trainer)) {
                                        $select_totalcommission = "SELECT 
                                                * ,SUM(c.`commission`) as total_commission FROM 
                                                tbl_user AS u
                                                JOIN tbl_commission AS c ON c.`trainee_id` = u.`id`
                                                JOIN tbl_user_details AS ud ON c.`trainee_detail_id` = ud.`id`
                                                where u.reffered_by = 'a_".$row['affiliate_id']."'
                                                and ud.`is_refund` = '0' AND u.`is_active` =  1  AND u.`is_paid` = 1 " ;
                                        $query_totalcommission = mysql_query($select_totalcommission) or die(mysql_error());
                                        $data_totalcommission = mysql_fetch_assoc($query_totalcommission);   
                                        $total_commission = $data_totalcommission['total_commission'];
                                        $select_paidamount = "SELECT SUM(total_amount) AS total_amount FROM tbl_aff_transfer  WHERE at_affilate_id= '" . $row['affiliate_id'] . "'";
                                        $query_paidamount = mysql_query($select_paidamount) or die(mysql_error());
                                        $total_paidamount = mysql_fetch_assoc($query_paidamount);
                                        $paid_amount = $total_paidamount['total_amount'];
                                        $pending_amount = $total_commission - $total_paidamount['total_amount'];
                                        // $total_amount = $paid_amount + $pending_amount;



                                        $serial_number++;
                                        $bnk_string = '';
                                        if ($row['b_payment_type'] == 'paypal') {
                                            $bnk_string = '' . $row['b_payment_type'] . '
                                        <br/> ';
                                        } else if ($row['b_payment_type'] == 'bank') {
                                            $bnk_string = '' . $row['b_payment_type'] . '
																																	<br/>';
                                        }

                                        echo '

											<tr>

												<td>' . $row['affiliate_username'] . '</td>

												<td>' . $row['email_id'] . '</td>

												<td>' . $row['phone'] . '</td>
												
												<td>' . $row['affiliate_address'] . '</td>

												<td>' . $row['city'] . '</td>

												<td>' . $row['country'] . '</td>

												<td>' . $row['website_url'] . '</td>
                                                                                                <td>' . $row['create_ts'] . '</td>
                                                                                                <td>' . $total_commission . '</td>

												<td>' . $paid_amount . '</td>

												<td>' . $pending_amount . '</td>
												
												<td>' . $bnk_string . '</td>
												
												<td>' . $row['b_bank_name'] . '</td>
												
												<td>' . $row['b_bank_acc_no'] . '</td>
												
												<td>' . $row['b_bank_branch'] . '</td>
												
												<td>' . $row['b_pancard'] . '</td>

												<td>';

                                        if ($row['is_active']) {

                                            echo '<a class="" href="' . $_SERVER['PHP_SELF'] . '?aff_prg=1&del_id=' . $row['id'] . '" title="Delete" style="text-decoration:none;"><span class="label label-success">Active</span></a>';
                                        } else {

                                            echo '<a class="" href="' . $_SERVER['PHP_SELF'] . '?aff_prg=1&act_id=' . $row['id'] . '" title="Activate" style="text-decoration:none;"><span class="label label-default">Inactive</span></a>';
                                        }

                                        echo '</td>

													<td>

														<a class="btn btn-info" href="' . $_SERVER['PHP_SELF'] . '?aff_prg=1&id=' . $row['affiliate_id'] . '" title="Edit">

															<i class="fa fa-edit "></i>  

														</a>

													</td>

													<td>

														<a href="user_via_affilation.php?aff_prg=1&aff_id=' . $row['affiliate_id'] . '">View Info</a>

													</td>

											</tr>

										';

                                        $i++;
                                    }
                                } else {

                                    echo '<tr>

										<td colspan="9" style="text-align:center;">No Records Found</td>

									</tr>';
                                }
                                ?>						

                            </tbody>

                        </table>  

                        <?php
                        echo '<ul class="pagination">';

                        if ($rec_count > $rec_limit) {

                            if ($page > 0 && $left_rec > $rec_limit) {

                                $last = $page - 2;

                                echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?aff_prg=1&page=' . $last . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Prev</a></li> ';

                                echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?aff_prg=1&page=' . $page . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Next</a></li>';
                            } else if ($page == 0) {

                                echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?aff_prg=1&page=' . $page . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Next</a></li>';
                            } else if ($left_rec < $rec_limit || $left_rec == $rec_limit) {

                                $last = $page - 2;

                                echo '<li><a href="' . $_SERVER["PHP_SELF"] . '?aff_prg=1&page=' . $last . '&srch_value=' . $value . '&sel_get_limit=' . $rec_limit . '" class="anc_click">Prev</a></li>';
                            }
                        }

                        echo '</ul>';
                        ?>

                    </div>

                </div>

            </div><!--/col-->



        </div><!--/row-->





    </div><!-- End Box Div-->

</div><!-- End Middle Content Div-->

<?php include_once('footer.php'); ?>

<script>

                                            $ = jQuery.noConflict();

                                            $(document).ready(function() {

                                                $('.close').click(function() {

                                                    $('.alert-info').addClass('hide');

                                                });



                                                $('#edit_aff_user').click(function() {

                                                    document.add_aff_user_form.submit()

                                                });

                                            });





</script>